// var n;
// exports.HallMgr = void 0;
// var r;
// var $dataManager = require("./DataManager");
// var $logManager = require("./LogManager");
// var $arrayUtils = require("./ArrayUtils");
// var $playerDataManager = require("./PlayerDataManager");
// var $excel = require("./Excel");
// var $const_Hall = require("./Const_Hall");
// !(function (e) {
//     e.shop = "shop";
//     e.unLockCiTiao = "unLockCiTiao";
// })(r || (r = {}));
// var h = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t._secret_key = "hallSaveData";
//         t._unLockCiTiao = [];
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.init = function (e) {
//         if (void 0 === e) {
//             e = null;
//         }
//         this._shopData = new $const_Hall.ShopSaveData();
//         var t = this.getNowData(r.shop, e);
//         this._shopData = __assign(__assign({}, this._shopData), t);
//         var o = this.getNowData(r.unLockCiTiao, e);
//         if (o) {
//             this._unLockCiTiao = o;
//         }
//     };
//     t.prototype.getNowData = function (e, t) {
//         var o = null;
//         if (!(t && (o = t[e]))) {
//             o = this._load(e);
//         }
//         return o;
//     };
//     Object.defineProperty(t.prototype, "shopData", {
//         get: function () {
//             if (!this._shopData) {
//                 this._shopData = new $const_Hall.ShopSaveData();
//                 $logManager.LogMgr.error("出现严重错误，商店数据取到了空值");
//             }
//             return this._shopData;
//         },
//         set: function (e) {
//             this._shopData = e;
//         },
//         enumerable: !1,
//         configurable: !0
//     });
//     t.prototype.SaveShopData = function () {
//         this._save(r.shop, this._shopData);
//     };
//     t.prototype.CheckNextResetTime = function () {
//         var e = new Date().getTime();
//         if (0 == this.shopData.nextResetTime || e >= this.shopData.nextResetTime) {
//             this.ResetShopData();
//         }
//         return Math.floor((this.shopData.nextResetTime - e) / 1e3);
//     };
//     t.prototype.ResetShopData = function () {
//         var e = $excel.Excel.shopControl(1);
//         var t = 0;
//         var o = 0;
//         if (this._shopData) {
//             t = this._shopData.boxExp;
//             o = this._shopData.boxLevel;
//         }
//         this._shopData = new $const_Hall.ShopSaveData();
//         this._shopData.boxExp = t;
//         this._shopData.boxLevel = o;
//         var n = new Date();
//         n.setHours(e.resetTime, 0, 0, 0);
//         this.shopData.nextResetTime = n.getTime();
//         this.ResetTodayShop();
//     };
//     t.prototype.ResetTodayShop = function () {
//         this.shopData.todayShopArray = this.GetNewTodayShopData();
//         this.SaveShopData();
//     };
//     t.prototype.GetNewTodayShopData = function () {
//         var e = [2, 3];
//         var t = [];
//         var o = [];
//         var n = [[], []];
//         $excel.Excel.todayShop_all.forEach(function (e) {
//             if ($playerDataManager.PlayerDataMgr.GetEquipDataById(e.shuiguoId).isUnLock) {
//                 o.push(e);
//                 n[0].push(e.goldWeight);
//                 n[1].push(e.weight);
//             }
//         });
//         for (var i = 0; i < e.length; i++) {
//             for (var a = 0; a < e[i]; a++) {
//                 var r = o[$arrayUtils.default.getWeightRandom(n[i])];
//                 var c = new $const_Hall.TodayShopItemData();
//                 c.shopId = r.id;
//                 c.richType = i + 1;
//                 c.canBuyCount = 1;
//                 c.discount = r.discount[Math.floor(Math.random() * r.discount.length)];
//                 if (i + 1 == $const_Hall.RichType.金币) {
//                     c.price = Math.floor(r.gold * c.discount);
//                 } else {
//                     c.price = Math.floor(r.diamond * c.discount);
//                 }
//                 t.push(c);
//                 c.price;
//             }
//         }
//         return t;
//     };
//     t.prototype.GetCiTiaoIsLock = function (e) {
//         return -1 != this._unLockCiTiao.indexOf(e);
//     };
//     t.prototype.SetCiTiaoIsLock = function (e) {
//         if (-1 == this._unLockCiTiao.indexOf(e)) {
//             this._unLockCiTiao.push(e);
//             this._save(r.unLockCiTiao, this._unLockCiTiao);
//         }
//     };
//     return t;
// })($dataManager.default);
// exports.HallMgr = h.getInstance();


// import DataManager from "./DataManager";
// import { LogMgr } from "./LogManager";
// import ArrayUtils from "./ArrayUtils";
// import { PlayerDataMgr } from "./PlayerDataManager";
// import { Excel } from "./Excel";
// import { ShopSaveData, TodayShopItemData, RichType } from "./Const_Hall";

// enum HallDataType {
//     shop = "shop",
//     unLockCiTiao = "unLockCiTiao"
// }

// /**
//  * 大厅管理类
//  * 建议类名：HallManager
//  */
// class HallMgr extends DataManager {
//     private _secretKey: string = "hallSaveData";
//     private _unLockCiTiao: string[] = [];
//     private _shopData: ShopSaveData | null = null;

//     /**
//      * 初始化方法
//      * @param data 初始化数据
//      * 建议方法名：initialize
//      */
//     public init(data: any = null): void {
//         this._shopData = new ShopSaveData();
//         const shopData = this.getNowData(HallDataType.shop, data);
//         this._shopData = { ...this._shopData, ...shopData };

//         const unLockCiTiaoData = this.getNowData(HallDataType.unLockCiTiao, data);
//         if (unLockCiTiaoData) {
//             this._unLockCiTiao = unLockCiTiaoData;
//         }
//     }

//     /**
//      * 获取当前数据
//      * @param type 数据类型
//      * @param data 数据
//      * @returns 当前数据
//      * 建议方法名：getCurrentData
//      */
//     private getNowData(type: HallDataType, data: any): any {
//         let result = null;
//         if (!(data && (result = data[type]))) {
//             result = this._load(type);
//         }
//         return result;
//     }

//     /**
//      * 获取商店数据
//      * @returns 商店数据
//      * 建议方法名：getShopData
//      */
//     public get shopData(): ShopSaveData {
//         if (!this._shopData) {
//             this._shopData = new ShopSaveData();
//             LogMgr.error("出现严重错误，商店数据取到了空值");
//         }
//         return this._shopData;
//     }

//     /**
//      * 设置商店数据
//      * @param data 商店数据
//      * 建议方法名：setShopData
//      */
//     public set shopData(data: ShopSaveData) {
//         this._shopData = data;
//     }

//     /**
//      * 保存商店数据
//      * 建议方法名：saveShopData
//      */
//     public SaveShopData(): void {
//         this._save(HallDataType.shop, this._shopData);
//     }

//     /**
//      * 检查下次重置时间
//      * @returns 剩余时间（秒）
//      * 建议方法名：checkNextResetTime
//      */
//     public CheckNextResetTime(): number {
//         const currentTime = new Date().getTime();
//         if (this.shopData.nextResetTime === 0 || currentTime >= this.shopData.nextResetTime) {
//             this.ResetShopData();
//         }
//         return Math.floor((this.shopData.nextResetTime - currentTime) / 1000);
//     }

//     /**
//      * 重置商店数据
//      * 建议方法名：resetShopData
//      */
//     public ResetShopData(): void {
//         const shopControlData = Excel.shopControl(1);
//         let boxExp = 0;
//         let boxLevel = 0;

//         if (this._shopData) {
//             boxExp = this._shopData.boxExp;
//             boxLevel = this._shopData.boxLevel;
//         }

//         this._shopData = new ShopSaveData();
//         this._shopData.boxExp = boxExp;
//         this._shopData.boxLevel = boxLevel;

//         const resetDate = new Date();
//         resetDate.setHours(shopControlData.resetTime, 0, 0, 0);
//         this.shopData.nextResetTime = resetDate.getTime();

//         this.ResetTodayShop();
//     }

//     /**
//      * 重置今日商店
//      * 建议方法名：resetTodayShop
//      */
//     public ResetTodayShop(): void {
//         this.shopData.todayShopArray = this.GetNewTodayShopData();
//         this.SaveShopData();
//     }

//     /**
//      * 获取新的今日商店数据
//      * @returns 今日商店数据数组
//      * 建议方法名：getNewTodayShopData
//      */
//     public GetNewTodayShopData(): TodayShopItemData[] {
//         const richTypes = [2, 3];
//         const todayShopData: TodayShopItemData[] = [];
//         const availableItems: any[] = [];
//         const weights: number[][] = [[], []];

//         Excel.todayShop_all.forEach(item => {
//             if (PlayerDataMgr.GetEquipDataById(item.shuiguoId).isUnLock) {
//                 availableItems.push(item);
//                 weights[0].push(item.goldWeight);
//                 weights[1].push(item.weight);
//             }
//         });

//         for (let i = 0; i < richTypes.length; i++) {
//             for (let j = 0; j < richTypes[i]; j++) {
//                 const selectedItem = availableItems[ArrayUtils.getWeightRandom(weights[i])];
//                 const newItem = new TodayShopItemData();
//                 newItem.shopId = selectedItem.id;
//                 newItem.richType = i + 1;
//                 newItem.canBuyCount = 1;
//                 newItem.discount = selectedItem.discount[Math.floor(Math.random() * selectedItem.discount.length)];
//                 newItem.price = i + 1 === RichType.金币
//                     ? Math.floor(selectedItem.gold * newItem.discount)
//                     : Math.floor(selectedItem.diamond * newItem.discount);
//                 todayShopData.push(newItem);
//             }
//         }

//         return todayShopData;
//     }

//     /**
//      * 判断词条是否解锁
//      * @param ciTiao 词条
//      * @returns 是否解锁
//      * 建议方法名：isCiTiaoUnlocked
//      */
//     public GetCiTiaoIsLock(ciTiao: string): boolean {
//         return this._unLockCiTiao.indexOf(ciTiao) !== -1;
//     }

//     /**
//      * 设置词条解锁
//      * @param ciTiao 词条
//      * 建议方法名：unlockCiTiao
//      */
//     public SetCiTiaoIsLock(ciTiao: string): void {
//         if (this._unLockCiTiao.indexOf(ciTiao) === -1) {
//             this._unLockCiTiao.push(ciTiao);
//             this._save(HallDataType.unLockCiTiao, this._unLockCiTiao);
//         }
//     }
// }

// export const HallMgrInstance = HallMgr.getInstance();

import DataManager from "./DataManager";
import { LogMgr } from "./LogManager";
import ArrayUtils from "./ArrayUtils";
import { PlayerDataMgr } from "./PlayerDataManager";
import { Excel } from "./Excel";
import { ShopSaveData, TodayShopItemData, RichType } from "./Const_Hall";

/**
 * 大厅管理类
 * 建议类名：HallManager
 */
enum HallDataType {
    shop = "shop",
    unLockCiTiao = "unLockCiTiao"
}

class HallManager extends DataManager {
    private _secretKey: string = "hallSaveData";
    private _unLockCiTiao: string[] = [];
    private _shopData: ShopSaveData | null = null;

    /**
     * 初始化方法
     * @param data 初始化数据
     * 建议方法名：initialize
     */
    public initialize(data: any = null): void {
        this._shopData = new ShopSaveData();
        const shopData = this.getCurrentData(HallDataType.shop, data);
        this._shopData = { ...this._shopData, ...shopData };

        const unLockCiTiaoData = this.getCurrentData(HallDataType.unLockCiTiao, data);
        if (unLockCiTiaoData) {
            this._unLockCiTiao = unLockCiTiaoData;
        }
    }

    /**
     * 获取当前数据
     * @param type 数据类型
     * @param data 数据
     * @returns 当前数据
     * 建议方法名：getCurrentData
     */
    private getCurrentData(type: HallDataType, data: any): any {
        let result = null;
        if (!(data && (result = data[type]))) {
            result = this._load(type);
        }
        return result;
    }

    /**
     * 获取商店数据
     * @returns 商店数据
     * 建议方法名：getShopData
     */
    public get shopData(): ShopSaveData {
        if (!this._shopData) {
            this._shopData = new ShopSaveData();
            LogMgr.error("出现严重错误，商店数据取到了空值");
        }
        return this._shopData;
    }

    /**
     * 设置商店数据
     * @param data 商店数据
     * 建议方法名：setShopData
     */
    public set shopData(data: ShopSaveData) {
        this._shopData = data;
    }

    /**
     * 保存商店数据
     * 建议方法名：saveShopData
     */
    public saveShopData(): void {
        this._save(HallDataType.shop, this._shopData);
    }

    /**
     * 检查下次重置时间
     * @returns 剩余时间（秒）
     * 建议方法名：checkNextResetTime
     */
    public checkNextResetTime(): number {
        const currentTime = new Date().getTime();
        if (this.shopData.nextResetTime === 0 || currentTime >= this.shopData.nextResetTime) {
            this.resetShopData();
        }
        return Math.floor((this.shopData.nextResetTime - currentTime) / 1000);
    }

    /**
     * 重置商店数据
     * 建议方法名：resetShopData
     */
    public resetShopData(): void {
        const shopControlData = Excel.shopControl(1);
        let boxExp = 0;
        let boxLevel = 0;

        if (this._shopData) {
            boxExp = this._shopData.boxExp;
            boxLevel = this._shopData.boxLevel;
        }

        this._shopData = new ShopSaveData();
        this._shopData.boxExp = boxExp;
        this._shopData.boxLevel = boxLevel;

        const resetDate = new Date();
        resetDate.setHours(shopControlData.resetTime, 0, 0, 0);
        this.shopData.nextResetTime = resetDate.getTime();

        this.ResetTodayShop();
    }

    /**
     * 重置今日商店
     * 建议方法名：resetTodayShop
     */
    public ResetTodayShop(): void {
        this.shopData.todayShopArray = this.getNewTodayShopData();
        this.saveShopData();
    }

    /**
     * 获取新的今日商店数据
     * @returns 今日商店数据数组
     * 建议方法名：getNewTodayShopData
     */
    public getNewTodayShopData(): TodayShopItemData[] {
        const richTypes = [2, 3];
        const todayShopData: TodayShopItemData[] = [];
        const availableItems: any[] = [];
        const weights: number[][] = [[], []];

        Excel.todayShop_all.forEach(item => {
            if (PlayerDataMgr.getEquipDataById(item.shuiguoId).isUnLock) {
                availableItems.push(item);
                weights[0].push(item.goldWeight);
                weights[1].push(item.weight);
            }
        });

        for (let i = 0; i < richTypes.length; i++) {
            for (let j = 0; j < richTypes[i]; j++) {
                const selectedItem = availableItems[ArrayUtils.getWeightedRandomIndex(weights[i])];
                const newItem = new TodayShopItemData();
                newItem.shopId = selectedItem.id;
                newItem.richType = i + 1;
                newItem.canBuyCount = 1;
                newItem.discount = selectedItem.discount[Math.floor(Math.random() * selectedItem.discount.length)];
                newItem.price = i + 1 === RichType.Gold
                    ? Math.floor(selectedItem.gold * newItem.discount)
                    : Math.floor(selectedItem.diamond * newItem.discount);
                todayShopData.push(newItem);
            }
        }

        return todayShopData;
    }

    /**
     * 判断词条是否解锁
     * @param ciTiao 词条
     * @returns 是否解锁
     * 建议方法名：isCiTiaoUnlocked
     */
    public isCiTiaoUnlocked(ciTiao: string): boolean {
        return this._unLockCiTiao.indexOf(ciTiao) !== -1;
    }

    /**
     * 设置词条解锁
     * @param ciTiao 词条
     * 建议方法名：unlockCiTiao
     */
    public unlockCiTiao(ciTiao: string): void {
        if (this._unLockCiTiao.indexOf(ciTiao) === -1) {
            this._unLockCiTiao.push(ciTiao);
            this._save(HallDataType.unLockCiTiao, this._unLockCiTiao);
        }
    }
}

export const HallMgr = HallManager.getInstance();